<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input v-model="search.username" placeholder="请输入要查询的用户名"></el-input>
      </el-col>
      <el-button @click="dosearch()" type="primary" icon="el-icon-search">搜索</el-button>
      <el-button type="primary" icon="el-icon-circle-plus-outline" @click="add()">添加用户</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="batchdel()">批量删除</el-button>
    </el-row>
    <br>
    <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column label="id" width="50" type="selection"></el-table-column>
      <el-table-column label="id" width="100" prop="id"></el-table-column>
      <el-table-column label="用户名" width="180" prop="username"></el-table-column>
      <el-table-column label="真实姓名" width="180" prop="realname"></el-table-column>
      
      <el-table-column label="最后一次登录" width="180" prop="lastlogin"></el-table-column>
      <el-table-column label="关联角色" width="100" prop="">
        <template slot-scope="scope">
          <el-link type="primary" @click="gorole(scope.row.id)">所属角色</el-link>
        </template>
      </el-table-column>
      <el-table-column label="是否有效" width="100" prop="">
        <template slot-scope="scope">
          <span v-if="scope.row.active === 1">有效</span>
          <span v-else>无效</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <template v-if="scope.row.active">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
          <template v-else>
            <el-button size="mini" type="success" @click="handleBack(scope.row.id)">恢复</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top:20px"
      background
      layout="prev, pager, next"
      :page-count="pages"
      :current-page.sync="query.pageNo"
      :page-size="query.pageSize"
      @current-change="getData"
    ></el-pagination>
    <el-dialog :title="title" :visible.sync="show" :close-on-click-modal="false">
      <useredit v-if="show" :show.sync="show" :editid="editid" @getdata="getData()"></useredit>
    </el-dialog>
  </div>
</template>

<script>
import useredit from '@/views/user/edit'
export default {
    name: 'User',
    data(){
        return {
            tableData: [],
            query:{
                username: '',
                pageSize: 6,
                pageNo: 1,
            },
            search:{
                username: ''
            },
            pages: 0,
            show: false,
            editid : null,
            multipleSelection: [],
            title: '',
            activeHz: ['无效','有效']
            
        }
    },
    created() {
        this.getData()
    },
    methods: {
        getData(){
            this.axios.get('/ums-user/list',response =>{
                this.tableData=response.obj.records
                this.pages=response.obj.pages
            },this.query)
        
        },
        dosearch(){
            this.query.username = this.search.username
            this.query.pageNo = 1
            this.getData()
        },
        handleEdit(id)
        {
            this.title='修改用户'
            this.editid = id
            this.show = true
        },
        add(){
          this.title='添加用户'
            this.editid = ''
            this.show = true
        },
        handleDelete(id) {
          this.axios.del('/ums-user/del',response =>{
            this.getData()
          },{'id': id})
      },
      handleSelectionChange(val)
      {
        this.multipleSelection = val
      },
      batchdel()
      {
        if(this.multipleSelection.length===0)
        {
          this.$message("没有任何被选中的数据")
        }
        else{
          const ids=[]
          for(let i=0 ;i<this.multipleSelection.length;i++)
          {
            ids.push(this.multipleSelection[i].id)
          }
           this.axios.del('/ums-user/batchdel',response =>{
            this.getData()
          },{'ids':ids})
        }
      },
      handleBack(id)
      {
        this.axios.del('/ums-user/back',response =>{
            this.getData()
          },{'id': id},'back')
      },
      gorole(id)
      {
        this.$router.push({
          path: 'user-role',
          query:{
            id: id
          }
        })

      }
    },
    components:{
        useredit
    }
}

</script>

<style>
</style>